package solved;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.PriorityQueue;

/*
ID:		liluyan1
LANG:	JAVA
TASK:	shopping
*/
public class shopping {
	public static void main(String[] args) throws IOException {
		BufferedReader f = new BufferedReader(new FileReader("shopping.in"));
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
				"shopping.out")));
		new shopping().solve(f, out);
		out.close();
		System.exit(0);
	}
	
	private Discount[]	discounts;
	
	private int[] codes;
	private int[] needs;
	private int[] prices;
	private int INF = 1000*25;
	
	private HashMap<String, Integer> dp;
	
	private void solve(BufferedReader f, PrintWriter out) throws IOException {
		int N = read(f);
		discounts = new Discount[N];
		for (int i=0; i<N; i++) {
			String[] s = f.readLine().split(" ");
			int count = Integer.parseInt(s[0]);
			int price = Integer.parseInt(s[s.length - 1]);
			discounts[i] = new Discount(price);
			for (int j=0; j<count; j++) {
				discounts[i].addItem(Integer.parseInt(s[2*j+1]), 
						Integer.parseInt(s[2*j+2]));
			}
		}
		
		int M = read(f);
		
		codes = new int[5];
		needs = new int[5];
		prices = new int[5];
		for (int i=0; i<M; i++) {
			int[] a = read(f, 3);
			codes[i] = a[0];
			needs[i] = a[1];
			prices[i] = a[2];
		}
		
		dp = new HashMap<String, Integer>();
		List<int[]>[] lists = new ArrayList[26];
		for (int i=0; i<26; i++) lists[i] = new ArrayList<int[]>();
		lists[0].add(new int[5]);
		dp.put("00000", 0);
		
		for (int i=0; i<25; i++) {
			for (int[] a : lists[i]) {
				int cost = cost(key(a));
				for (int j=0; j<M; j++) addItem(lists, a, j, cost);
				for (int j=0; j<N; j++) addDiscount(lists, a, j, cost);
			}
		}
		
		int[] a = new int[5];
		for (int i=0; i<M; i++) a[i] = needs[i];
		out.println( cost(key(a)) );
	}
	
	private String key(int[] a) {
		StringBuilder sb = new StringBuilder();
		for (int i : a) sb.append(i);
		return sb.toString();
	}
	
	private int cost(String s) {
		return dp.containsKey(s)? dp.get(s) : INF;
	}
	
	private void addItem(List<int[]>[] lists, int[] a, int i, int cost) {
		if (a[i] == needs[i]) return;
		int[] b = Arrays.copyOf(a, 5);
		b[i]++;
		String keyB = key(b);
		int value = prices[i] + cost;
		if (value < cost(keyB)) {
			dp.put(keyB, value);
			int sum = 0;
			for (int j : b) sum += j;
			lists[sum].add(b);
		}
	}
	
	private void addDiscount(List<int[]>[] lists, int[] a, int i, int cost) {
		ArrayList<Item> batch = discounts[i].batch;
		int[] b = Arrays.copyOf(a, 5);
		for (Item item : batch) {
			int j = findItem(item.code);
			b[j] += item.count;
			if (b[j] > needs[j]) return;
		}
		String key = key(b);
		int value = cost + discounts[i].price;
		if (value < cost(key)) {
			dp.put(key, value);
			int sum = 0;
			for (int j : b) sum += j;
			lists[sum].add(b);
		}
	}
	
	private int findItem(int code) {
		for (int i=0; i<5; i++) if (code == codes[i]) return i;
		return -1;
	}
	
	private class Discount {
		ArrayList<Item> batch;
		int price;
		
		public Discount(int price) {
			this.price = price;
			this.batch = new ArrayList<Item>();
		}
		
		public void addItem(int code, int count) {
			batch.add(new Item(code, count));
		}
	}
	
	private class Item {
		int code;
		int count;
		public Item(int code, int count) {
			this.code = code;
			this.count = count;
		}
	}
	
	public int read(BufferedReader f) throws IOException {
		return Integer.parseInt(f.readLine());
	}
	
	public int[] read(BufferedReader f, int N) throws IOException {
		String[] t = f.readLine().split(" ");
		int[] a = new int[N];
		for (int i=0; i<N; i++) {
			a[i] = Integer.parseInt(t[i]);
		}
		return a;
	}
}
